/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package oop.demo.restaurantmanagementsystem;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Restaurant billing implementation that handles 
 * customer orders and bill calculation
 * @author ashongtical
 */

public class RestaurantBilling extends RestaurantBillingSystem {
    
    
    //Constructor to initialize order list and meal quantities map
    //
    ArrayList<Meal> order;
    private HashMap<String,Integer> mealQuantities;
     
    public RestaurantBilling(){
        this.order = new ArrayList<>();
        this.mealQuantities = new HashMap<>();
    }

    //Add a meal from the menu to the customer's order
    //mealName: The name of the meal to add
    //return true if meal was added successfully, false otherwise
    @Override
    public boolean addMealToOrder(String mealName){
        Meal meal = findMealByName(mealName);
        if(meal == null){   
            return false;
        }
        order.add(meal);
        mealQuantities.put(mealName,mealQuantities.getOrDefault(mealName,0) + 1);
        return true;
    }
    
    

    
    //Override the calculateBill() and calculate the total bill for the customer's order
    //return the total bill amount
    @Override
    public double calculatingBill(){
        double totalBill = 0.0;
        for(Meal meal : order){
            totalBill += meal.getPrice();
        }
        return totalBill;
    }
    
    
    //Clear the current order
    public void clearOrder(){
        order.clear();
        mealQuantities.clear();
    }
    
    
    //Display the current order with meal quantities and total price
    public void displayOrder(){
        if(order.isEmpty()){
            System.out.println("The menu is empty");
        }else{
            for(Meal meal : order){
                System.out.println(meal.getName() + "-" + mealQuantities.get(meal.getName()) + meal.getPrice() + "RMB");
            }
        }
    }
    
    
    //Generate a formatted bill for the customer
    //customerName: The name of the customer
    //return String representation of the bill
    public String generateBill(String customerName){
        String bill = "Bill for" + customerName + "\n";
        for(Meal meal : order){
            bill += meal.getName() + "-"  + mealQuantities.get(meal.getName()) + meal.getPrice() + "RMB";
        }
        bill += "Total:RMB" + calculatingBill();
        return bill;
    }
    
    
}
